package bhoost.shell;

import android.content.Context;
import android.graphics.Canvas;
import android.hardware.CameraDevice;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class Preview extends SurfaceView implements SurfaceHolder.Callback {
  SurfaceHolder  mHolder;
  private PreviewThread mPreviewThread;
  private boolean mHasSurface;
  private static int WIDTH;
  private static int HEIGHT;
  private Context con;

  String attributeNamespace = "http://schemas.android.com/apk/res/com.test1";
  
  public Preview(Context context, int width, int height) {
      super(context);
      con = context;
      WIDTH = width;
      HEIGHT = height;
      setFocusable(true);
      // Install a SurfaceHolder.Callback so we get notified when the
      // underlying surface is created and destroyed.

      mHolder = getHolder();
      mHolder.addCallback(this);
      mHasSurface = false;
      
      // In this example, we hardcode the size of the preview. In a real
      // application this should be more dynamic. This guarantees that
      // the uderlying surface will never change size.

//      mHolder.setFixedSize(320, 240);
      mHolder.setFixedSize(width, height);
  }
  
  public void resume() {
      // We do the actual acquisition in a separate thread. Create it now.
      if (mPreviewThread == null) {
          mPreviewThread = new PreviewThread();
          // If we already have a surface, just start the thread now too.
          if (mHasSurface == true) {
              mPreviewThread.start();
          }
      }
  }
  
  public void pause() {
      // Stop Preview.
      if (mPreviewThread != null) {
          mPreviewThread.requestExitAndWait();
          mPreviewThread = null;
      }
  }
  
  public Canvas capture() {
    if (mPreviewThread != null)
      return mPreviewThread.capture();
    return null;
  }

  public void onDraw(Canvas canvas) {
    
  }
  
  public void surfaceCreated(SurfaceHolder holder) {
      // The Surface has been created, start our main acquisition thread.
      mHasSurface = true;
      if (mPreviewThread != null) {
          mPreviewThread.start();
      }
  }

  public void surfaceDestroyed(SurfaceHolder holder) {
      // Surface will be destroyed when we return. Stop the preview.
      mHasSurface = false;
      pause();
  }

  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
      // Surface size or format has changed. This should not happen in this
      // example.
  }

  // ----------------------------------------------------------------------

  class PreviewThread extends Thread {
      private boolean mDone;
      int width = 1280;
      int height = 960;
//      private Bitmap bitmap;
      private Canvas canvas;
      
      PreviewThread() {
          super();
          mDone = false;
      }
  
      @Override
  public void run() {
          // We first open the CameraDevice and configure it.
          CameraDevice camera = CameraDevice.open();
//          bitmap = Bitmap.createBitmap(width, height, false);
          if (camera != null) {
              CameraDevice.CaptureParams param = new CameraDevice.CaptureParams();
                  param.type = 1; // preview
                  param.srcWidth      = 1280;
                  param.srcHeight     = 960;
                  param.leftPixel     = 0;
                  param.topPixel      = 0;
                  param.outputWidth   = WIDTH;
                  param.outputHeight  = HEIGHT;
                  param.dataFormat    = 2; // RGB_565
              camera.setCaptureParams(param);
          }

          // This is our main acquisition thread's loop, we go until
          // asked to quit.
          SurfaceHolder holder = mHolder;
          while (!mDone) {
              // Lock the surface, this returns a Canvas that can
              // be used to render into.
              canvas = holder.lockCanvas();
//              canvas.setDevice(bitmap);

              // Capture directly into the Surface
              if (camera != null) {
                  camera.capture(canvas);
              }
              
              // And finally unlock and post the surface.
              holder.unlockCanvasAndPost(canvas);
          }

          // Make sure to release the CameraDevice
          if (camera != null)
              camera.close();
      }
      
      public Canvas capture() {
        return canvas;
      }
      
      public void requestExitAndWait() {
          // don't call this from PreviewThread thread or it a guaranteed
          // deadlock!
          mDone = true;
          try {
              join();
          } catch (InterruptedException ex) { }
      }
  }
}